-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Sized Hierarchy: Part I #137944
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
davidtwco
wants to merge
30
commits into
rust-lang:master
Choose a base branch
from
davidtwco:sized-hierarchy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Sized Hierarchy: Part I #137944
+4,031
−1,035
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This was referenced Mar 3, 2025
This comment has been minimized.
This comment has been minimized.
|
This comment has been minimized.
This comment has been minimized.
73f1b4f
to
7f509ab
Compare
This comment has been minimized.
This comment has been minimized.
2537bfb
to
b31fd85
Compare
cc @rust-lang/lang |
Does this perhaps fix #127336 by rejecting it? |
This comment was marked as resolved.
This comment was marked as resolved.
b31fd85
to
2beed43
Compare
It doesn't currently. |
2beed43
to
839b844
Compare
Undrafting now that CI passes |
As before, add `MetaSized` and `PointeeSized` traits to all of the non-minicore `no_core` tests so that they don't fail for lack of language items.
As core uses an extern type (`ptr::VTable`), the default `?Sized` to `MetaSized` migration isn't sufficient, and some code that previously accepted `VTable` needs relaxed to continue to accept extern types. Similarly, the compiler uses many extern types in `rustc_codegen_llvm` and in the `rustc_middle::ty::List` implementation (`OpaqueListContents`) some bounds must be relaxed to continue to accept these types. Unfortunately, due to the current inability to relax `Deref::Target`, some of the bounds in the standard library are forced to be stricter than they ideally would be.
Adding a sizedness supertrait shouldn't require multiple vtables so shouldn't be linted against.
Opting-out of `Sized` with `?Sized` is now equivalent to adding a `MetaSized` bound, and adding a `MetaSized` or `PointeeSized` bound is equivalent to removing the default `Sized` bound - this commit implements this change in `rustc_hir_analysis::hir_ty_lowering`. `MetaSized` is also added as a supertrait of all traits, as this is necessary to preserve backwards compatibility. Unfortunately, non-global where clauses being preferred over item bounds (where `PointeeSized` bounds would be proven) - which can result in errors when a `PointeeSized` supertrait/bound/predicate is added to some items. Rather than `PointeeSized` being a bound on everything, it can be the absence of a bound on everything, as `?Sized` was.
With `MetaSized` bounds replacing `?Sized` and being added as a supertrait, the same relaxations applied to the standard library must be applied to minicore.
`Sized` errors are currently stashed to improve diagnostics and this must happen with `{Meta,Pointee}Sized` too to maintain diagnostic output.
Given the necessary additions of bounds to these traits and their impls in the standard library, it is necessary to add `MetaSized` bounds to the obligation which is proven as part of checking for dyn dispatchability.
Like `Sized` diagnostics, sorting `MetaSized` and `PointeeSized` diagnostics last prevents earlier more useful diagnostics from being skipped because there has already been error tainting.
When printing impl headers in a diagnostic, the compiler has to account for `?Sized` implying `MetaSized` and new `MetaSized` and `PointeeSized` bounds.
When `sized_hierarchy` is enabled, rustc should print `MetaSized` or `PointeeSized` instead of `?Sized` in opaques.
These tests necessarily need to change now that `?Sized` is not sufficient to accept extern types and `PointeeSized` is now necessary. In addition, the `size_of_val`/`align_of_val` test can now be changed to expect an error.
These tests expected entirely unconstrained parameters and their `?Sized` bounds are no longer sufficient to achieve that.
It seems like generics from `non_lifetime_binders` don't have any default bounds like normal generics, so all of the `?Sized` relaxations need to be further relaxed with `PointeeSized` for this test to be the equivalent of before.
This test no longer crashes the compiler as `Box` no longer accepts `PointeeSized`-types. It eventually could, but not because of `Deref::Target` currently, so this doesn't fail anymore and there wasn't an obvious to add new types to make it continue to fail because `Deref` is special.
These tests just need blessing, they don't have any interesting behaviour changes. Some of these tests have new errors because `LegacyReceiver` cannot be proven to be implemented now that it is also testing for `MetaSized` - but this is just a consequence of the other errors in the test.
Extend the fast path for `Sized` traits to include constness and `MetaSized`.
As a performance optimization, skip elaborating the supertraits of `Sized`, and if a `MetaSized` obligation is being checked, then look for a `Sized` predicate in the parameter environment. This makes the `ParamEnv` smaller which should improve compiler performance as it avoids all the iteration over the larger `ParamEnv`.
`nominal_obligations` calls `predicates_of` on a `Sized` obligation, effectively elaborating the trait and making the well-formedness checking machinery do a bunch of extra work checking a `MetaSized` obligation is well-formed, but given that both `Sized` and `MetaSized` are built-ins, if `Sized` is otherwise well-formed, so `MetaSized` will be.
These should never be shown to users at the moment.
Some rustdoc tests are `no_core` and need to have `MetaSized` and `PointeeSized` added to them.
As before, updating types using extern types to use `PointeeSized` bounds.
Unexpected Clippy lint triggering is fixed in upcoming commits but is necessary for `cfg(bootstrap)`.
Existing lints that had special-casing for `Sized` predicates ought to have these same special cases applied to `MetaSized` predicates.
One clippy test is `no_core` and needs to have `MetaSized` and `PointeeSized` added to it.
As in many previous commits, adding the new traits to minicore, but this time for cranelift and gcc.
It's unclear why this change in miri is necessary.
These error messages include lines of the standard library which have changed and so need updated.
Rebasing after #139577, just removing one commit, otherwise unchanged. |
fb4c0d9
to
9540d13
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
A-attributes
Area: Attributes (`#[…]`, `#![…]`)
A-run-make
Area: port run-make Makefiles to rmake.rs
A-rustdoc-json
Area: Rustdoc JSON backend
A-rustdoc-search
Area: Rustdoc's search feature
F-autodiff
`#![feature(autodiff)]`
perf-regression
Performance regression.
PG-exploit-mitigations
Project group: Exploit mitigations
S-waiting-on-review
Status: Awaiting review from the assignee but also interested parties.
T-bootstrap
Relevant to the bootstrap subteam: Rust's build system (x.py and src/bootstrap)
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
T-lang
Relevant to the language team, which will review and decide on the PR/issue.
T-libs
Relevant to the library team, which will review and decide on the PR/issue.
T-rustdoc
Relevant to the rustdoc team, which will review and decide on the PR/issue.
T-rustdoc-frontend
Relevant to the rustdoc-frontend team, which will review and decide on the web UI/UX output.
WG-trait-system-refactor
The Rustc Trait System Refactor Initiative (-Znext-solver)
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This patch implements the non-const parts of rust-lang/rfcs#3729. It introduces two new traits to the standard library,
MetaSized
andPointeeSized
. See the RFC for the rationale behind these traits and to discuss whether this change makes sense in the abstract.These traits are unstable (as is their constness), so users cannot refer to them without opting-in to
feature(sized_hierarchy)
. These traits are not behindcfg
s as this would make implementation unfeasible, there would simply be too manycfg
s required to add the necessary bounds everywhere. So, likeSized
, these traits are automatically implemented by the compiler.RFC 3729 describes changes which are necessary to preserve backwards compatibility given the introduction of these traits, which are implemented and as follows:
?Sized
is rewritten asMetaSized
MetaSized
is added as a default supertrait for all traits w/out an explicit sizedness supertrait already.There are no edition migrations implemented in this, as these are primarily required for the constness parts of the RFC and prior to stabilisation of this (and so will come in follow-up PRs alongside the const parts). All diagnostic output should remain the same (showing
?Sized
even if the compiler seesMetaSized
) unless thesized_hierarchy
feature is enabled.Due to the use of unstable extern types in the standard library and rustc, some bounds in both projects have had to be relaxed already - this is unfortunate but unavoidable so that these extern types can continue to be used where they were before. Performing these relaxations in the standard library and rustc are desirable longer-term anyway, but some bounds are not as relaxed as they ideally would be due to the inability to relax
Deref::Target
(this will be investigated separately).It is hoped that this is implemented such that it could be merged and these traits could exist "under the hood" without that being observable to the user (other than in any performance impact this has on the compiler, etc). Some details might leak through due to the standard library relaxations, but this has not been observed in test output.
Notes:
PointeeSized
is a different name from the RFC just to make it more obvious that it is different fromstd::ptr::Pointee
but all the names are yet to be bikeshed anyway.Fixes #79409.
r? @ghost (I'll discuss this with relevant teams to find a reviewer)